<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    
	
	<title>_异常单重发</title>
	<script type="text/javascript">
	
	//页面初始
    $(function() {
			
			$("#_customerCode").val("${customer.customerCode}");
			$("#_customerName").val("${customer.customerName}");
			
			$("#_warehouseCode").val("${warehouse.groupCode}");
			$("#_warehouseName").val("${warehouse.groupName}");
    });
	
	
	//查询商品
	function skuQuery(){
	
	    var sku=$("#sku").val().trim();
	    var customerCode=$("#_customerCode").val();
	    var warehouseCode=$("#_warehouseCode").val();
	    
	    
	    $("#sku_content").html('');
	    
	    $.ajax({
			type : 'post',
			url : '${ctx}/outstorageBill/ajaxQuerySku',
			data : {
				customerCode:customerCode,
				warehouseCode:warehouseCode,
				sku:sku
			},
			dataType : 'json',
			success : function(data) {
				var stockList=data.stockList;
				var str="";
				$.each(stockList,function(i,stock){
				
					str+="<tr>";
					str+="<td>"+(i+1)+"</td>";
					str+="<td id='customerCode_"+stock.sku+"'>"+stock.customer.customerCode+"</td>";
					str+="<td id='warehouseCode_"+stock.sku+"'>"+stock.warehouseCode+"</td>";
					str+="<td id='sku_"+stock.sku+"'>"+stock.sku+"</td>";
					str+="<td id='salesNum_"+stock.sku+"'>"+stock.salesNum+"</td>";
					
					//判断是否已经添加
					var flag='1';
					$("[name='add']").each(function(i){
					    var tempId=this.id;
					    if(tempId==stock.sku){
					        flag='2';
					    }
					});
					if(flag=='1'){
					   str+="<td>"+"<input type='button' class='btn btn-info'  value='添加' id='btn_"+stock.sku+"' onclick=\"add('"+stock.sku+"')\">"+"</td>";
					}else if(flag=='2'){
					   str+="<td>"+"<input type='button' class='btn'  value='已添加'  id='btn_"+stock.sku+"'>"+"</td>";
					}
					
					str+="</tr>";
				});
				$("#sku_content").html(str);
			},
			error:function() {
				alert("网络繁忙，请稍后重试...");
			}
		}); 
	}
	
	//添加商品
	function add(id){
	    var sku=$("#sku_"+id).html();
	    
	    var trElement="";
	    trElement+="<tr name='add' id='"+id+"'>";
	    trElement+="<td id='add_sku_"+id+"'>"+sku+"</td>";
	    trElement+="<td ><input type='text' id='add_num_"+id+"'/><span  id='add_message_"+id+"' style='color:red;'></span></td>";
	    trElement+="<td><input type='button' class='btn btn-info' value='取消' onclick='del("+id+");'/></td>";
	    trElement+="</tr>";
	    
	    $("#add_info").append(trElement);
	    
	    var btn=$("#btn_"+id);
	    btn.attr("disabled",true);
           btn.attr("class","btn");
           btn.val("已添加");
	    
	    $("#nodata").hide();
	}
	
	//取消商品
	function del(id){
	    $("#"+id).remove();
	    var btn=$("#btn_"+id);
	    btn.attr("disabled",false);
        btn.attr("class","btn  btn-info");
        btn.bind("click","add("+id+")");
        btn.val("添加");
	}
	
	
	//创建JSON对象
	function createObj(customerCode,warehouseCode,sku,num){
	    return {
	        customerCode:customerCode,
	        warehouseCode:warehouseCode,
	        sku:sku,
	        num:num
	    };
	}
	
		
	//创建订单
	function recall(){
		
		   //////////////////////////////////验证订单信息///////////////////////////
		   var cacode=$("#_cacode").val();
		   if(cacode==''){
		         alert("请选择快递公司");
		         return;
		   }
		   
		   var recena=$("#_recena").val().trim();
		   if(recena==''){
		         alert("请填写收件人姓名");
		         return;
		   }
		   
		   var teleph=$("#_teleph").val().trim();
		   var mobile=$("#_mobile").val().trim();
		   if(teleph=='' && mobile==''){
		         alert("请填写固定电话或者移动电话");
		         return;
		   }
		   
		   var addres=$("#_addres").val().trim();
		   if(addres==''){
		         alert("请填写收件地址");
		         return;
		   }
		   
		    var btn=$("#btn_recall");
		    btn.attr("disabled",true);
		    btn.attr("class","btn");
		   
		    ///////////////////////////商品信息//////////////////////////////////
		    var jsonArray=[];
		    var flag=true;
		    var flag_color=true;
		    
		    var customerCode=$("#_customerCode").val();
		    var warehouseCode=$("#_warehouseCode").val();
		    
		    //检查数据，并创建json对象
		    $("[name='add']").each(function(i){
		       var id=this.id;
		       var _sku=$("#add_sku_"+id).html();
		       var _num=$("#add_num_"+id).val().trim();
		       
		       if( isNaN(_num) || _num==''){
	               $("#add_message_"+id).html("请输入整数!");
	               flag=false;
	           }else{
	               $("#add_message_"+id).html("");
	           }
	           
	           var obj=createObj(_sku,_num);
	           jsonArray.push(obj);
	           
		    });
		    
		    if(jsonArray.length==0){
		       alert("请选择商品！");
		       btn.attr("disabled",false);
		       btn.attr("class","btn btn-primary");
		       return;
		    }
		    
		    if(flag==false || flag_color==false){
		      btn.attr("disabled",false);
		      btn.attr("class","btn btn-primary");
		      return;
		    }
		    
		    var jsonList= eval(jsonArray); 
		    var jsonText= JSON.stringify(jsonList);
		   
		   $.ajax({
				type : 'post',
				url : '${ctx}/v2/backOrder/ajaxRecall',
				data:{
					cacode:cacode,
					recena:recena,
					mobile:mobile,
					teleph:teleph,
					addres:addres,
					jsonText:jsonText,
					exceptionId:'${exceptionOrder.id}'
				},
				dataType : 'json',
				success : function(data) {
				
				      var msg=data.msg;
				      if(msg=="success"){
				          window.alert("重发成功！！请按“确定”继续。"); 
				          location.href='${ctx}/v2/backOrder/show';
				      }else if(msg == "stockNotEnough"){
				      	  alert("库存不足..");
				      }else{
						  alert("网络繁忙，请稍后重试...");
				      }
				      
				      btn.attr("disabled",false);
	    		      btn.attr("class","btn btn-warning");
				    
				},
				error : function() {
					btn.attr("disabled",false);
		    		btn.attr("class","btn btn-warning");
					alert("网络繁忙，请稍后重试...");
				}
			});
		
		}
		
		//创建JSON对象
		function createObj(sku,num){
		    return {
		        sku:sku,
		        num:num
		    };
		}
	
    </script>
</head>

<body>
<section class="content">
    <!-- 查询条件  start-->
   	<div class="row">
	<div class="col-md-12">

			<!-- Primary box -->
			<div class="box box-solid box-primary" >
			    
				<div class="box-header">
					<i class="fa fa-edit"></i>
					<h3 class="box-title">异常单重发</h3>
				</div>
				
				<div class="box-body">
	  <table id="contentTable" class="table  table-bordered table-condensed">
		
  <tr><td>

    <div class="tabbable">
    
	  	   <ul class="nav nav-tabs">
			   <li class="${tab1}"><a href="#tab1" data-toggle="tab">订单信息</a></li>
			   <li class="${tab2}"><a href="#tab2" data-toggle="tab">选择商品</a></li>
		   </ul>
		
		   <div class="tab-content">
		   
		        <!-- 订单信息 -->
				<div class="tab-pane ${tab1}" id="tab1">
				  
					<!-- 收件信息 -->
					<table class="table table-striped table-bordered table-condensed" style="margin-top: 0px;">
						<thead>
							<th style="vertical-align: middle; color: white; " colspan="6" bgcolor="#8EB4CB">收件信息</th>
						</thead>
						<tr>
						   <td style="vertical-align: middle;width: 90px; text-align: center;">收件人姓名：</td>
						   <td style="width: 200px;">
						       <input  type="text" id="_recena" value="${ediOrder.recena}" class="input-medium" />
						   </td>
						   
						   <td style="vertical-align: middle;  width: 90px; text-align: center;">移动电话：</td>
						   <td style="width: 200px;">
						       <input  type="text" id="_mobile"  value="${ediOrder.mobile}" class="input-medium"/>
						   </td>
						   
						   <td style="vertical-align: middle;  width: 90px; text-align: center;">固定电话：</td>
						   <td style="width: 200px;">
						       <input  type="text" id="_teleph" value="${ediOrder.teleph}" class="input-medium"/>
						   </td>
						   
					   </tr>
					   
					   <tr>
						   
						   <td style="vertical-align: middle;  width: 90px; text-align: center;">快递公司：</td>
						   <td style="width: 200px;">
						       <select class="input-medium"  id="_cacode" name="_cacode"  value="${ediOrder.cacode}" style="width: 165px;">
						        <option value=""></option>
								<c:forEach items="${cmpList}" var="cmp">
									<option value="${cmp.companyCode}"
									  <c:if test="${ediOrder.cacode eq cmp.companyCode }">selected=selected</c:if>
									>${cmp.companyName}</option>
								</c:forEach>
						</select> 
						   </td>
						   
						  
						   
						   <td style="vertical-align: middle; width: 90px; text-align: center;" >详细地址：</td>
						   <td colspan="3">
						      <input  type="text" id="_addres" value="${ediOrder.addres}"  style="width: 400px;"/>
						   </td>
					   </tr>
					   
					</table>
					
				</div>
				
				<!-- 选择商品-->
				<div class="tab-pane ${tab2}" id="tab2">
				
				<form class="form-search" >
				<table id="contentTable" class="table  table-bordered table-condensed">
				    <thead>
						<th style="vertical-align: middle; color: white;" colspan="13" bgcolor="#8EB4CB">选中的商品</th>
					</thead>
					<thead><tr>
					    <th>sku</th>
					    <th>数量</th>
						<th>操作</th>
					</tr></thead>
					<c:if test="${empty itemList or itemList.size() eq 0}">
				       <tr id="nodata"><td colspan="6" height="50" style="vertical-align:middle; text-align: center;color: gray;font-size: 20px;">无已选商品</td></tr>
				    </c:if>
					<tbody id="add_info">
					   
					     <c:forEach items="${itemList}" var="g" varStatus="i">
							<tr name="add" id="${g.sku}">
							   <td id="add_sku_${g.sku}">${g.sku}</td>
							   <td>
							     <input type="text" id="add_num_${g.sku}" value="${g.qty}"/>
							     <span  id="add_message_${g.sku}" style='color:red;'></span>
							   </td>
							   <td><input type="button" class="btn btn-info" value="取消" onclick="del('${g.sku}');"/></td>
							</tr>
						 </c:forEach>
					
					</tbody>
			    </table>
			    </form>
			    
				<!-- 商品查询 -->
				<form class="form-search">
				<table id="contentTable" class="table table-striped table-bordered table-condensed">
				    <thead>
						<th style="vertical-align: middle; color: white; " colspan="13" bgcolor="#8EB4CB">商品查询</th>
					</thead>
					
					<tr><td colspan="6">
					    
					     <label style="margin-left: 10px;">商家名称：</label>
					     <input  type="text" id="_customerName" class="input-medium" readonly="readonly"/>
					     <input type="hidden" id="_customerCode" />
						
					   <label style="margin-left: 10px;" >作业仓库：</label>
					   <input  type="text" id="_warehouseName" class="input-medium"  readonly="readonly"/>
					   <input type="hidden" id="_warehouseCode" />
					
					    <label>SKU编码：</label> 
						<input type="text"  id="sku"  class="input-medium" />
					    <button type="button" class="btn btn-info" style="margin-left: 10px;" onclick="skuQuery();">搜索</button>
			        </td></tr>
					
					<thead><tr>
					    <th>序号</th>
					    <th>商家</th>
					    <th>仓库</th>
					    <th>sku</th>
					    <th>可用库存</th>
						<th>操作</th>
					</tr></thead>
					<tbody id="sku_content">
					</tbody>
				</table>
				</form>
				
	         </div>
	         
	         
	     </div>
	 </div>
    
    </td></tr>
  </table>
  
	</div>
	</div>
	</div>
	</div>
	<div class="modal-footer bg-gray">
  <input type="button" id="btn_recall" value="确定重发" class="btn btn-warning" style="margin-left:10px;" onclick="recall();"/>
  </div>
	</section>
</body>
</html>
